import Game from "./Game";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Fruit extends cc.Component {
	// @property(cc.Label)
	id: Number = 0;
	private gameLogic: Game = null;
	// onLoad () {}

	start() {}

	init(data) {
		this.gameLogic = data.gameLogic;
		this.id = data.id;
		// 根据传入的参数修改贴图资源
		const sp = this.node.getComponent(cc.Sprite);
		sp.spriteFrame = data.iconSF;
		this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEendClick, this);
	}

	// 点击水果
	onTouchEendClick() {
		// 如果使用了道具则消除
		if (this.gameLogic.bUsedDelProp) {
			this.node.removeFromParent();
			this.gameLogic.bUsedDelProp = false;
		}
	}

	onBeginContact(contact, self, other) {
		if (self.tag != other.tag) return;

		// 检测到是两个相同水果的碰撞
		if (self.node && other.node) {
			self.node['isCreate'] = true;
			other.node['isCreate'] = true;
			const s = self.node.getComponent('Fruit');
			const o = other.node.getComponent('Fruit');
			if (s && o && s.id === o.id) {
				self.node.emit('sameContact', {self, other});
			}
		}
	}

	onDestroy(): void {
		this.node.removeFromParent(false);
	}
	// update (dt) {}
}
